﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace YTCMS.Core
{
    public class ValidatePwd
    {
        /// <summary>
        /// 判断密码长度是否符合要求
        /// 合法密码定义：长度6-25位，须包含大写、小写、数字、特殊符号中的任意3种
        /// </summary>
        /// <param name="pass"></param>
        /// <param name="minLength"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static string Valid(string password, string title = "密码", int minLength = 6, int maxLength = 25)
        {
            //用户名密码正确，判断密码是否符合新要求
            Regex r = new Regex(@"(?x)^((?<p1>\d)|(?<p2>[a-z])|(?<p3>[A-Z])|(?<p4>[\W_]))+(?(p1)(?(p2)(?(p3)|(?(p4)|(?!)))|(?(p3)(?(p4)|(?!))|(?!)))|(?(p2)(?(p3)(?(p4)|(?!))|(?!))|(?!)))$");
            //密码不规则（长度不够或不包含三种字符），此情况直接显示修改密码弹出层，要求修改密码
            //if (pass.Length < minLength || pass.Length > maxLength || !r.IsMatch(pass))
            //{
            //    return false;
            //} 



            if (string.IsNullOrEmpty(password))
            {
                return "请输入" + title + "！";
            }
            if (password.IndexOf(" ") >= 0)
            {
                return title + "不能输入空格！";
            }
            if (password.Length < minLength || password.Length > maxLength)
            {
                return title + "长度有误，请输入6-25位字符！";
            }
            if (!ValidatePwd.ValidPass(password))
            {
                return title + "须包含字母和数字！";
            }
            return "";
        }
        /// <summary>
        /// 验证密码 需包含数字和字母的密码验证规则
        /// </summary>
        /// <returns></returns>
        public static bool ValidPass(string pass)
        {
            Regex regsNum = new Regex("[0-9]");
            Regex regsChar = new Regex("[A-Za-z]");
            bool t1 = regsNum.IsMatch(pass);
            bool t2 = regsChar.IsMatch(pass);
            if (regsNum.IsMatch(pass) && regsChar.IsMatch(pass))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
